package com.dumengwei.cms_monthmn.controller;

import java.math.BigDecimal;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dumengwei.cms_monthmn.entity.Company;
import com.dumengwei.cms_monthmn.entity.Patent;
import com.dumengwei.cms_monthmn.service.PatentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class PatentController {
	@Autowired
	private PatentService patentService;
	
	@RequestMapping("list")
	public String list(@RequestParam(defaultValue="1")Integer pageNum,Patent patent,Model model){
		BigDecimal Sumsales1 =new BigDecimal("0.00");
		BigDecimal Sumsales2 =new BigDecimal("0.00");
		BigDecimal Sumrewards = new BigDecimal("0.00");
		Patent ze = patentService.ze();
		Sumsales1=Sumsales1.add(ze.getSumSales1());
		Sumsales2=Sumsales2.add(ze.getSumSales2());
		Sumrewards=Sumrewards.add(ze.getSumRewards());
		 model.addAttribute("Sumsales1", Sumsales1);
		 model.addAttribute("Sumsales2", Sumsales2);
		 model.addAttribute("Sumrewards", Sumrewards);
		 System.out.println(Sumsales1+""+Sumsales2+""+Sumrewards);
		PageHelper.startPage(pageNum, 2);
		List<Patent>list=patentService.list();
		PageInfo<Patent> pageInfo = new PageInfo<>(list);
		model.addAttribute("pg", pageInfo);
		model.addAttribute("list", list);
		return "list";
	}
	
	@RequestMapping("toUpdate")
	public String toUpdate(Integer id,Model model){
		Patent pa=patentService.findPatentById(id);
		List<Company> list = patentService.listCom();
		model.addAttribute("list", list);
		model.addAttribute("pa", pa);
		return "update";
	}
	
	@RequestMapping("update")
	public String update(@Valid @ModelAttribute("pa")Patent pa,BindingResult result){
		patentService.update(pa);
		return "redirect:list";
	}
	@RequestMapping("toAdd")
	public String toAdd(Model model){
		List<Company> list = patentService.listCom();
		model.addAttribute("list", list);
		model.addAttribute("pa", new Patent());
		
		return "add";
	}
	
	@RequestMapping("add")
	public String add(@Valid @ModelAttribute("pa")Patent pa,BindingResult result){
		patentService.add(pa);
		return "redirect:list";
	}
	
	@RequestMapping("listCom")
	@ResponseBody
	public Object listCom(){
		List<Company> list = patentService.listCom();
		for (Company c : list) {
			System.out.println(c);
		}
		return list;
	}
	
	@RequestMapping("mingxi")
	public String mingxi(Model model){
		List<Patent> list = patentService.mingxi();
		model.addAttribute("list", list);
		return "jiangli";
	}
}
